package com.l024.framework.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.l024.framework.R;

/**
 * @author wu_ming_zhi_bei
 * @date 2020/2/27 12:14
 * @Notes  图片验证
 */
public class TouchPictureView extends View {

    //背景图
    private Bitmap bgBitmap;
    //背景画笔
    private Paint mBgPaint;
    //空白块
    private Bitmap mNullBitmap;
    //空白画笔
    private Paint mNullPaint;
    //移动方块
    private Bitmap mMoveBitmap;
    //移动画笔
    private Paint mMovePaint;
    //view的宽高
    private int mWidth,mHeight;
    //空白方块的宽高
    private int cardSize;
    //空白方块的位置
    private int nullCardX,nullCardY;
    //移动方块的x坐标
    private int moveCardX;
    //误差值
    private int errorValues = 10;
    //移动监听
    private OnViewResultListener onViewResultListener = null;

    public void setOnViewResultListener(OnViewResultListener onViewResultListener) {
        this.onViewResultListener = onViewResultListener;
    }

    public TouchPictureView(Context context) {
        super(context);
        init();
    }

    public TouchPictureView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TouchPictureView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mBgPaint = new Paint();
        mNullPaint = new Paint();
        mMovePaint = new Paint();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth =w;
        mHeight = h;
        moveCardX = w/4;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制背景
        drawBg(canvas);
        //绘制空的方块
        drawNullCard(canvas);
        //绘制移动方块
        drawMoveCard(canvas);
    }

    /**
     * 绘制背景
     * @param canvas
     */
    private void drawBg(Canvas canvas){
        //获取背景图
        Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img_bg);
        //创建一个空白的Bitmap
        bgBitmap = Bitmap.createBitmap(mWidth,mHeight,Bitmap.Config.ARGB_8888);
        //将图片绘制到空白Bitmap
        Canvas bgCanvas = new Canvas(bgBitmap);
        bgCanvas.drawBitmap(mBitmap,null,new Rect(0,0,mWidth,mHeight),mBgPaint);
        //将bgBitmap绘制到View上
        canvas.drawBitmap(bgBitmap,null,new Rect(0,0,mWidth,mHeight),mBgPaint);
    }

    /**
     * 绘制空白方块
     */
    private void drawNullCard(Canvas canvas){
        //获取背景图
        mNullBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img_null_card);
        cardSize = mNullBitmap.getWidth();
        nullCardX = mWidth/3*2;
        nullCardY = mHeight/2-(cardSize/2);
        canvas.drawBitmap(mNullBitmap,nullCardX,nullCardY,mNullPaint);
    }

    /**
     * 编写移动方块
     */
    private void drawMoveCard(Canvas canvas){
        //截取空白位置坐标的Bitmap图像
        mMoveBitmap =  Bitmap.createBitmap(bgBitmap,nullCardX,nullCardY,cardSize,cardSize);
        //绘制在View上
        canvas.drawBitmap(mMoveBitmap,moveCardX,nullCardY,mMovePaint);
    }

    /**
     * 滑动事件
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            //屏幕按下
            case MotionEvent.ACTION_DOWN:{
                //判断点击是否是方块内部，如果是就可以拖动
                int x = (int) event.getX();
                int y = (int) event.getY();
                if(y>=nullCardY&&y<=(nullCardY+cardSize)&&x>=(mWidth/4)&&x<=((mWidth/4)+cardSize)){
                    return true;
                }else{
                    return false;
                }
            }
            case MotionEvent.ACTION_MOVE:{
                //方块移动
                //防止越界
                if(event.getX()>0&&event.getX()<(mWidth-cardSize)){
                    moveCardX = (int)event.getX();
                    //刷新
                    invalidate();
                }
                break;
            }
            case MotionEvent.ACTION_UP:{
                int x = (int)event.getX();
                if(x>=(nullCardX-errorValues)&&x<(nullCardX+errorValues)){
                    if(onViewResultListener!=null){
                        onViewResultListener.onResult(true);
                    }
                }else{
                    if(onViewResultListener!=null){
                        onViewResultListener.onResult(false);
                    }
                    moveCardX = mWidth/4;
                    invalidate();
                }
                break;
            }
        }
        return true;
    }

    public interface OnViewResultListener{
        void onResult(boolean isSuccess);
    }
}
